﻿#pragma once

#include "AppOperators.h"

namespace frmApplication {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace Microsoft::Ink;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class frmMain : public System::Windows::Forms::Form
	{
	public:
		frmMain(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~frmMain()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::GroupBox^  groupBoxDraw;



	private: bool isMouseDown;
	private: InkCollector^ myInkCollector;
	private: System::Windows::Forms::Button^  btn_Delete;
	private: System::Windows::Forms::Label^  lbl_X;
	private: System::Windows::Forms::Label^  lbl_Y;




	private: System::Windows::Forms::Button^  btn_Recognize;
	private: System::Windows::Forms::Button^  btn_Train;
	private: System::Windows::Forms::TextBox^  tb_Result;


	private: System::Windows::Forms::Label^  lbl_Result;


	




	protected: 
		
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->groupBoxDraw = (gcnew System::Windows::Forms::GroupBox());
			this->btn_Delete = (gcnew System::Windows::Forms::Button());
			this->lbl_X = (gcnew System::Windows::Forms::Label());
			this->lbl_Y = (gcnew System::Windows::Forms::Label());
			this->btn_Recognize = (gcnew System::Windows::Forms::Button());
			this->btn_Train = (gcnew System::Windows::Forms::Button());
			this->tb_Result = (gcnew System::Windows::Forms::TextBox());
			this->lbl_Result = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// groupBoxDraw
			// 
			this->groupBoxDraw->Location = System::Drawing::Point(34, 12);
			this->groupBoxDraw->Name = L"groupBoxDraw";
			this->groupBoxDraw->Size = System::Drawing::Size(662, 389);
			this->groupBoxDraw->TabIndex = 0;
			this->groupBoxDraw->TabStop = false;
			this->groupBoxDraw->Text = L"Written Region";
			this->groupBoxDraw->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &frmMain::groupBoxDraw_MouseDown);
			this->groupBoxDraw->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &frmMain::groupBoxDraw_MouseUp);
			this->groupBoxDraw->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &frmMain::groupBoxDraw_MouseMove);
			// 
			// btn_Delete
			// 
			this->btn_Delete->Location = System::Drawing::Point(138, 463);
			this->btn_Delete->Name = L"btn_Delete";
			this->btn_Delete->Size = System::Drawing::Size(94, 30);
			this->btn_Delete->TabIndex = 3;
			this->btn_Delete->Text = L"Delete";
			this->btn_Delete->UseVisualStyleBackColor = true;
			this->btn_Delete->Click += gcnew System::EventHandler(this, &frmMain::btn_Delete_Click);
			// 
			// lbl_X
			// 
			this->lbl_X->AutoSize = true;
			this->lbl_X->Location = System::Drawing::Point(661, 430);
			this->lbl_X->Name = L"lbl_X";
			this->lbl_X->Size = System::Drawing::Size(13, 13);
			this->lbl_X->TabIndex = 1;
			this->lbl_X->Text = L"0";
			// 
			// lbl_Y
			// 
			this->lbl_Y->AutoSize = true;
			this->lbl_Y->Location = System::Drawing::Point(704, 430);
			this->lbl_Y->Name = L"lbl_Y";
			this->lbl_Y->Size = System::Drawing::Size(13, 13);
			this->lbl_Y->TabIndex = 2;
			this->lbl_Y->Text = L"0";
			// 
			// btn_Recognize
			// 
			this->btn_Recognize->Location = System::Drawing::Point(138, 417);
			this->btn_Recognize->Name = L"btn_Recognize";
			this->btn_Recognize->Size = System::Drawing::Size(94, 30);
			this->btn_Recognize->TabIndex = 4;
			this->btn_Recognize->Text = L"Recognize";
			this->btn_Recognize->UseVisualStyleBackColor = true;
			this->btn_Recognize->Click += gcnew System::EventHandler(this, &frmMain::btn_Recognize_Click);
			// 
			// btn_Train
			// 
			this->btn_Train->Location = System::Drawing::Point(12, 417);
			this->btn_Train->Name = L"btn_Train";
			this->btn_Train->Size = System::Drawing::Size(94, 30);
			this->btn_Train->TabIndex = 5;
			this->btn_Train->Text = L"Train";
			this->btn_Train->UseVisualStyleBackColor = true;
			this->btn_Train->Click += gcnew System::EventHandler(this, &frmMain::btn_Train_Click);
			// 
			// tb_Result
			// 
			this->tb_Result->Location = System::Drawing::Point(419, 427);
			this->tb_Result->Name = L"tb_Result";
			this->tb_Result->Size = System::Drawing::Size(166, 20);
			this->tb_Result->TabIndex = 6;
			// 
			// lbl_Result
			// 
			this->lbl_Result->AutoSize = true;
			this->lbl_Result->Location = System::Drawing::Point(366, 430);
			this->lbl_Result->Name = L"lbl_Result";
			this->lbl_Result->Size = System::Drawing::Size(37, 13);
			this->lbl_Result->TabIndex = 7;
			this->lbl_Result->Text = L"Result";
			// 
			// frmMain
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(751, 505);
			this->Controls->Add(this->lbl_Result);
			this->Controls->Add(this->tb_Result);
			this->Controls->Add(this->btn_Train);
			this->Controls->Add(this->btn_Recognize);
			this->Controls->Add(this->btn_Delete);
			this->Controls->Add(this->lbl_Y);
			this->Controls->Add(this->lbl_X);
			this->Controls->Add(this->groupBoxDraw);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
			this->MaximizeBox = false;
			this->Name = L"frmMain";
			this->Text = L"Online HandWritten Application";
			this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &frmMain::frmMain_UnLoad);
			this->Load += gcnew System::EventHandler(this, &frmMain::frmMain_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void frmMain_Load(System::Object^  sender, System::EventArgs^  e) {
				 isMouseDown = false;
				 myInkCollector = gcnew InkCollector(this->groupBoxDraw->Handle);
				 myInkCollector->DefaultDrawingAttributes->Width = 50;
				 myInkCollector->Enabled = true;
				 Init();
				 LoadData();
			 }
	private: System::Void frmMain_UnLoad(System::Object^  sender,  System::Windows::Forms::FormClosedEventArgs^ e) {
				 Destroy();
			 }
	private: System::Void groupBoxDraw_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^ e ) {
			
				 isMouseDown = true;
				 int mouseX = e->X;
				 int mouseY = groupBoxDraw->Height - e->Y;
				 AddMouseDown();
				 AddMouseMove(mouseX, mouseY);
			 }

	private: System::Void groupBoxDraw_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^ e ) {
				 if (!isMouseDown)
					 return;
				 if (e->X < 0 || e->Y < 0 || e->X > groupBoxDraw->Width || e->Y > groupBoxDraw->Height)
					 return;
				 
				int mouseX = e->X;
				int mouseY = groupBoxDraw->Height - e->Y;
				this->lbl_X->Text = System::Convert::ToString(mouseX);
				this->lbl_Y->Text = System::Convert::ToString(mouseY);
				AddMouseMove(mouseX, mouseY);
				
			 }
	private: System::Void groupBoxDraw_MouseUp(System::Object^  sender, System::Windows::Forms::MouseEventArgs^ e ) {
				 isMouseDown = false;
				// groupBoxDraw->Invalidate();
				AddMouseUp();
			 }
	
private: System::Void btn_Delete_Click(System::Object^  sender, System::EventArgs^  e) {
			if (!myInkCollector->CollectingInk)
			{
				myInkCollector->Ink->DeleteStrokes(myInkCollector->Ink->Strokes);
				groupBoxDraw->Refresh();
				Init();
			}
		 }
private: System::Void btn_Train_Click(System::Object^  sender, System::EventArgs^  e) {
			PrintPoint();
		 }

private: System::Void btn_Recognize_Click(System::Object^  sender, System::EventArgs^  e) {
			 int iClass = Recognize();
			 tb_Result->Text = iClass.ToString("D");
		 }
};
}

